package com.itheima.reggie.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/10/30 19:17
 * @question:
 */

@Configuration
@Slf4j
//开启Swagger和Knife4j的功能
@EnableSwagger2
@EnableKnife4j
public class SpringmvcConfig implements WebMvcConfigurer {

    @Autowired
    //注入拦截器
    private LoginInterceptor loginInterceptor;

    /**
     * 设置静态资源映射
     * 此些没在static当中,必须进行映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器 并设置拦截路径(拦截所有)
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(
                //设置不需要拦截的进行排除
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/error",
                //配置上之后 无需登录也能上传/下载图片
                "/common/**",
                //用户登录/用户发送短信
                "/user/sendMsg",
                "/user/login",
                //添加swagger的无需处理
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"
        );
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        log.info("扩展消息转换器...");
        //创建自己想用的消息处理器
        MappingJackson2HttpMessageConverter mj = new MappingJackson2HttpMessageConverter();
        //注入自己的消息处理器
        mj.setObjectMapper(new JacksonObjectMapper());
        //框架中也有自己的处理器,想让自己的首先生效将其排到最前边即可
        converters.add(0,mj);
    }

    /**
     * swagger文档的一些简易说明等
     * @return
     */
    @Bean
    public Docket createRestApi() {
        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.itheima.reggie.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("瑞吉外卖")
                .version("1.0")
                .description("瑞吉外卖接口文档")
                .build();
    }



}
